<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" :isDisplaySearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <!-- <el-form-item prop="code" v-if="editFlag">
          <label>项目</label>
          <el-input v-model="queryParam.qry_projectname" :disabled="true" class="tl-ref-selector" style="width:200px">
            <el-button slot="append" type="primary" icon="el-icon-search" @click="openPmProjectRefDialog"></el-button>
          </el-input>
        </el-form-item> -->
        <el-form-item label="项目名称">
          <el-input v-model="queryParam.qry_projectName" @change="changename"></el-input>
        </el-form-item>
        <el-form-item label="项目编号">
          <el-input v-model="queryParam.qry_projectCode" style="width:130px" @change="changecode"></el-input>
        </el-form-item>
        <el-form-item label="项目经理">
          <el-input v-model="queryParam.qry_projectManager" placeholder="姓名/工号" style="width:130px" @change="changeprojectmanagername"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <label>文件名称</label>
          <el-input v-model="queryParam.qry_displayname" placeholder="" style="width:150px" @change="changedisplayname"></el-input>
        </el-form-item>
        <el-form-item v-if="editFlag">
          <label>项目权限</label>
          <el-select v-model="queryParam.qry_permissiontype" filterable clearable style="width:120px" @change="changepermissiontype">
            <el-option v-for="item in codeModel.permissiontypeList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="editFlag">
          <label>项目类型</label>
          <el-select v-model="queryParam.qry_datatype" filterable clearable style="width:120px" @change="changedatatype">
            <el-option v-for="item in codeModel.datatypeList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="doQuery">搜索</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-refresh-right" @click="resetQueryParam" v-if="editFlag" :disabled="isrest">重置</el-button>
      </template>
    </tl-list-search>
    <!-- 工具栏 -->
    <tl-list-toolbar ref="toolbar" v-if="editFlag" @add="openAddDialog('editForm')" @exportExcel="doExportExcel" :isDisplayExport="false" :isDisplayDeleteBatch="false" @deleteBatch="doDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0">
      <template slot="left-btn"> </template>
    </tl-list-toolbar>
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="index" label="序号" width="100px" align="center"></el-table-column>
        <el-table-column v-if="editFlag" prop="dispsourceid" label="项目" width="250px" sortable="custom" sort-by="sourceid" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-if="editFlag" prop="code" label="项目编号" width="140px" sortable="custom" sort-by="code" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-if="editFlag" prop="projectmanagername" label="项目经理" width="120px" sortable="custom" sort-by="projectmanagername" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-if="editFlag" prop="projectmanagercode" label="项目经理工号" width="110px" sortable="custom" sort-by="projectmanagercode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="displayname" label="文件名称" width="300px" sortable="custom" sort-by="displayname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispextendfield1" label="权限" width="150px" sortable="custom" sort-by="extendfield1" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispextendfield2" label="资料类型" width="120px" sortable="custom" sort-by="extendfield2" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="uploaddate" label="上传时间" width="180px" sortable="custom" sort-by="uploaddate" align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="100px" v-slot="scope">
          <!-- <el-button type="text" icon="el-icon-edit" @click="openEditDialog('editForm', scope.row.id)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" @click="doDelete(scope.row.id)">删除</el-button> -->
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
              <el-dropdown-item icon="el-icon-edit" @click.native="openEditDialog('editForm', scope.row.id)" :disabled="!editFlag">编辑</el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete" @click.native="doDelete(scope.row.id)" :disabled="!editFlag">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tl-edit ref="editForm" @refresh="doRefresh" :projectId="projectId" :editFlag="editFlag"></tl-edit>
      <tl-pm-project-ref ref="pmProjectRef" @confirm="getPmProjectRefData" :isSingleSelect="true"></tl-pm-project-ref>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
import { validateNull } from '@tapui/utils/validate'
/** 子组件 */
import tlPmProjectRef from '@/views/ProjectManage/pmProject/refList'
import tlEdit from './edit'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'pmAttachment',
  mixins: [mixin_list],
  components: { tlPmProjectRef, tlEdit },
  data() {
    return {
      description: '项目资料-列表查询',
      urlPrefix: '/pm/attachment/',
      url: {
        queryCodeList: '/pm/attachment/dataCodeList'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'CboAttachment.createon',
        sord: 'desc'
      },
      isrest: false,
      queryParam: {
        qry_projectid: this.projectId
      },
      // 当前页面的编辑状态
      editFlag: true,
      reduceHeight: 325
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    // 模块是否可以编辑
    moduleEditFlag: {
      type: Number,
      default: function() {
        return 1
      }
    },
    // 当前登录人角色 PM--项目经理 PP--项目参数人 OTHER--其他
    currentRole: {
      type: String,
      default: function() {
        return 'PM'
      }
    }
  },
  methods: {
    changename() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_projectName !== '') {
        this.isrest = false
      } else {
        this.isrest = true
      }
    },
    changecode() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_projectCode !== '') {
        this.isrest = false
      } else {
        this.isrest = true
      }
    },
    changeprojectmanagername() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_projectManager !== '') {
        this.isrest = false
      } else {
        this.isrest = true
      }
    },
    changedisplayname() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_displayname !== '') {
        this.isrest = false
      } else {
        this.isrest = true
      }
    },
    changepermissiontype() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_permissiontype !== '') {
        this.isrest = false
      } else {
        this.isrest = true
      }
    },
    changedatatype() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_datatype !== '') {
        this.isrest = false
      } else {
        this.isrest = true
      }
    },
    afterLoadData() {
      this.isrest = false
      if (typeof this.queryParam.qry_projectName === 'undefined' && typeof this.queryParam.qry_projectCode === 'undefined' &&
        typeof this.queryParam.qry_projectManager === 'undefined' && typeof this.queryParam.qry_displayname === 'undefined' &&
        typeof this.queryParam.qry_permissiontype === 'undefined' && typeof this.queryParam.qry_datatype === 'undefined') {
        this.isrest = true
      }
    },
    /** 打开项目参照 */
    openPmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getPmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.queryParam, 'qry_projectid', id)
      this.$set(this.queryParam, 'qry_projectname', name)
    },
    resetQueryParam() {
      this.queryParam = {}
      this.changename()
      this.changecode()
      this.changeprojectmanagername()
      this.changedisplayname()
      this.changepermissiontype()
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      if (this.rowDbClickActionType === 'edit' && this.editFlag) {
        this.openEditDialog('editForm', row.id)
      } else {
        this.openViewDialog('editForm', row.id)
      }
    },
    initParam() {
      this.editFlag = this.moduleEditFlag === 1 && this.currentRole === 'PM'
      this.reduceHeight = this.editFlag ? 134 : 325
    },
    searchList(projectId) {
      if (!this.editFlag) {
        this.queryParam.qry_projectid = projectId
        this.loadData({})
      }
    },
    /** 表格列排序 */
    doSortChange(columnInfo, prop, order) {
      if (columnInfo.column === null) {
        return
      }
      // 不排序
      if (validateNull(columnInfo.order)) {
        Object.assign(this.sorterInfo, this.sorterDefaultInfo)
        this.loadData({})
        return
      }
      // 判断是否处理垃圾数据
      if (columnInfo.column.sortBy.indexOf('.') !== -1) {
        columnInfo.column.sortBy = columnInfo.column.sortBy.substring(columnInfo.column.sortBy.lastIndexOf('.') + 1)
      }
      if (columnInfo.prop.indexOf('.') !== -1) {
        columnInfo.prop = columnInfo.prop.substring(columnInfo.prop.lastIndexOf('.') + 1)
      }
      if (columnInfo.column.sortBy === 'code' || columnInfo.column.sortBy === 'projectmanagercode' || columnInfo.column.sortBy === 'projectmanagername') {
        // 升序或者降序排序
        if (columnInfo.column.sortBy) {
          columnInfo.column.sortBy = 'PmProject.' + columnInfo.column.sortBy
          this.sorterInfo.sidx = columnInfo.column.sortBy
        } else {
          columnInfo.prop = 'PmProject.' + columnInfo.prop
          this.sorterInfo.sidx = columnInfo.prop
        }
      } else {
        // 升序或者降序排序
        if (columnInfo.column.sortBy) {
          columnInfo.column.sortBy = 'CboAttachment.' + columnInfo.column.sortBy
          this.sorterInfo.sidx = columnInfo.column.sortBy
        } else {
          columnInfo.prop = 'CboAttachment.' + columnInfo.prop
          this.sorterInfo.sidx = columnInfo.prop
        }
      }
      this.sorterInfo.sord = columnInfo.order.replace('ending', '')
      this.loadData({})
    }
  },
  watch: {
    projectId(newValue, oldValue) {
      this.searchList(newValue)
    }
  },
  mounted() {
    this.initParam()
  }
}
</script>

<style scoped></style>
